package com.varian.system.convert;

import com.varian.log.model.ApiLogger;
import com.varian.system.dto.*;
import com.varian.system.entity.*;
import com.varian.system.enums.DictValueTypeEnum;
import com.varian.system.enums.NoticeReceiveTypeEnum;
import com.varian.system.vo.*;
import com.varian.task.model.TaskCreateModel;
import com.varian.task.model.TaskUnprocessModel;
import com.varian.tool.Func;
import org.mapstruct.*;

/**
 * @author ben
 * @since 2024/7/1
 */
@Mapper(
        componentModel = MappingConstants.ComponentModel.SPRING,
        imports = {
                Func.class,
                DictValueTypeEnum.class,
                NoticeReceiveTypeEnum.class
        })
public interface SystemConvert {

    @Mappings({
    })
    SysUserVO toVO(SysUser user);

    @Mappings({
            @Mapping(target = "avatar", ignore = true),
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "lastLoginIp", ignore = true),
            @Mapping(target = "lastLoginTime", ignore = true),
            @Mapping(target = "password", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysUser toEntity(SysUserDTO dto);

    @Mappings({
            @Mapping(target = "avatar", ignore = true),
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "lastLoginIp", ignore = true),
            @Mapping(target = "lastLoginTime", ignore = true),
            @Mapping(target = "password", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysUser toEntity(Long id, SysUserDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysRole toEntity(SysRoleDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysRole toEntity(Long id, SysRoleDTO dto);

    @Mappings({})
    SysRoleVO toVO(SysRole role);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysMenu toEntity(SysMenuDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysMenu toEntity(Long id, SysMenuDTO dto);

    @Mappings({
            @Mapping(target = "children", ignore = true),
    })
    @Named("toVO")
    SysMenuVO toVO(SysMenu menu);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "defaultItem", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "dictRenderType", ignore = true),
            @Mapping(target = "dictValue", ignore = true),
            @Mapping(target = "dictWeight", constant = "0"),
            @Mapping(target = "parentId", expression = "java(Func.TREE_ROOT_ID)"),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysDict toEntity(SysDictTypeDTO dto);

    @Mappings({})
    SysDictVO toVO(SysDict dict);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "defaultItem", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "dictRenderType", ignore = true),
            @Mapping(target = "dictValue", ignore = true),
            @Mapping(target = "dictValueType", ignore = true),
            @Mapping(target = "dictWeight", ignore = true),
            @Mapping(target = "parentId", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysDict toEntity(Long id, SysDictTypeDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "dictCode", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "dictValueType", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysDict toEntity(SysDictItemDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "dictCode", ignore = true),
            @Mapping(target = "dictValueType", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateName", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    SysDict toEntity(Long id, SysDictItemDTO dto);

    @Mappings({})
    ApiLogItemVO toVO(ApiLog apiLog);

    @Mappings({
            @Mapping(target = "id", source = "apiLog.id"),
            @Mapping(target = "traceId", source = "apiLog.traceId")
    })
    ApiLogVO toVO(ApiLog apiLog, ApiErrorLog apiErrorLog);

    @Mappings({
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    ApiLog toEntity(ApiLogger apiLogger);


    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "noticeStatus", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "noticeReceiverIds", source = "noticeReceiverIds", conditionExpression = "java(NoticeReceiveTypeEnum.USER.equals(dto.getNoticeReceiveType()))"),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysNotice toEntity(SysNoticeDTO dto);

    @Mappings({})
    SysNoticeVO toVO(SysNotice notice);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "noticeStatus", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "noticeReceiverIds", source = "dto.noticeReceiverIds", conditionExpression = "java(NoticeReceiveTypeEnum.USER.equals(dto.getNoticeReceiveType()))"),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "updateName", ignore = true)
    })
    SysNotice toEntity(Long id, SysNoticeDTO dto);

    @Mappings({})
    SysBizExceptionVO toVO(SysBizException bizException);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateName", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    SysBizException toEntity(SysBizExceptionDTO dto);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateName", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    SysBizException toEntity(Long id, SysBizExceptionDTO dto);

    @Mappings({
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "deptLevel", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "leftValue", ignore = true),
            @Mapping(target = "rightValue", ignore = true),
    })
    SysDept toEntity(SysDeptDTO dto);

    @Mappings({
            @Mapping(target = "children", ignore = true)
    })
    SysDeptTreeVO toTreeVO(SysDept dept);

    @Mappings({})
    SysDeptVO toVO(SysDept dept);

    @Mappings({@Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "deptLevel", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "leftValue", ignore = true),
            @Mapping(target = "rightValue", ignore = true),
            @Mapping(target = "deptStatus", ignore = true)
    })
    SysDept toEntity(Long id, SysDeptDTO dto);

    @Mappings({
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "executeTime", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "taskStatus", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true),
            @Mapping(target = "errorMessage", ignore = true)
    })
    SysTask toEntity(TaskCreateModel task);

    @Mappings({
            @Mapping(target = "taskId", source = "id")
    })
    TaskUnprocessModel toUnprocessModel(SysTask task);

    @Mappings({})
    SysTaskVO toVO(SysTask sysTask);

    @Mappings({
            @Mapping(target = "children", ignore = true)
    })
    SysDeptTreeItemDTO toTreeDTO(SysDept sysDept);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateName", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    SysParam toEntity(SysParamDTO dto);

    @Mappings({})
    SysParamVO toVO(SysParam param);

    @Mappings({
            @Mapping(target = "createId", ignore = true),
            @Mapping(target = "createName", ignore = true),
            @Mapping(target = "createTime", ignore = true),
            @Mapping(target = "deleted", ignore = true),
            @Mapping(target = "updateId", ignore = true),
            @Mapping(target = "updateName", ignore = true),
            @Mapping(target = "updateTime", ignore = true),
            @Mapping(target = "version", ignore = true)
    })
    SysParam toEntity(Long id, SysParamDTO dto);
}
